package com.example.demo.controller;


import com.example.demo.mapper.UserMapper;
import com.example.demo.pojo.Comment;
import com.example.demo.pojo.User;
import com.example.demo.service.Impl.CommentServiceImpl;
import com.example.demo.service.Impl.ConnectionServiceImpl;
import com.example.demo.util.Unite;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
public class CommentController {

    @Autowired
    CommentServiceImpl commentService;
    @Autowired
    UserMapper userMapper;

    @Autowired
    HttpServletRequest request;



    @GetMapping("/getComments")
    public Unite getComments(@RequestParam int question_id){
        List<Comment> list=commentService.getCommentByQID(question_id);
        return new Unite(list,true);
    }
    @GetMapping("/insertComment")
    public Unite insertComment(@RequestParam String content, @RequestParam int question_id){
        int user_id=(Integer) request.getSession().getAttribute("user");
        Comment comment=new Comment();
        comment.setContent(content);
        comment.setQuestion_id(question_id);
        commentService.insertComment(comment,user_id);
        return new Unite(commentService.getCommentByQID(question_id),true);
    }
    @GetMapping("/deleteComment")
    public Unite deleteComment(@RequestParam int id){
        commentService.removeById(id);
        return new Unite();
    }
    @GetMapping("/getAllComments")
    public Unite getAllComments(){
        List<Comment> list=commentService.getAllComments();
        return new Unite(list,true);
    }
}
